package com.example.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;

public class QuizActivity extends AppCompatActivity {

    private static final String TAG = "QuizActivity";
    private static final String KEY_INDEX = "INDEX";
    private static final String KEY_QUESTIONSTATE = "QuestionState";
    private static final String KEY_CORRECTNUM = "CorrectNum";
    private static final String KEY_ANSWERNUM = "AnswerNum";
    private static final String KEY_PROBLEM = "Problem";
    private static final int REQUEST_CODE_CHEAT = 0;

    private Button mTrueButton;
    private Button mFalseButton;
    private Button mCheatButton;
    private Button mSubmitButton;
    private ImageButton mNextButton;
    private ImageButton mPrevButton;
    private TextView mQuestionTextView;

    ArrayList<Question> mQuestionBank = new ArrayList<Question>();

//    private Question[] mQuestionBank = new Question[]{
//            new Question(R.string.question_australia, true),
//            new Question(R.string.question_oceans, true),
//            new Question(R.string.question_mideast, false),
//            new Question(R.string.question_africa, false),
//            new Question(R.string.question_americas, true),
//            new Question(R.string.question_asia, true)
//    };

//    ArrayList<Boolean> mQuestionBankstateArrayList = new ArrayList<Boolean>();
    boolean[] mQuestionBankstate ;           /*= new boolean[]{true,true,true,true,true,true,true,true,true,true};*/
    private int mCurrentIndex = 0;
    private boolean mIsCheater;
    private int mCorrectNum = 0;
    private int mAnswerNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        mQuestionBank = (ArrayList<Question>) getIntent().getSerializableExtra("key");
        mQuestionBankstate = new boolean[mQuestionBank.size()];
        for(int i =0;i<mQuestionBank.size();i++){               /*length*/
            mQuestionBankstate[i]=true;
        }

        super.onCreate(savedInstanceState);
        Log.d(TAG,"onCreate(Bundle) called");
        setContentView(R.layout.activity_quiz);

        if(savedInstanceState != null){
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX,0);
            mQuestionBankstate = savedInstanceState.getBooleanArray(KEY_QUESTIONSTATE);
            mCorrectNum = savedInstanceState.getInt(KEY_CORRECTNUM,0);
            mAnswerNum = savedInstanceState.getInt(KEY_ANSWERNUM,0);
            mQuestionBank = (ArrayList<Question>) savedInstanceState.getSerializable(KEY_PROBLEM);
        }

        mQuestionTextView =(TextView) findViewById(R.id.question_text_view);
        mQuestionTextView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                mCurrentIndex = (mCurrentIndex+1)%mQuestionBank.size();/*length;*/
                checkButtonEnable();
                updateQuestion();
            }
        });

        mTrueButton = findViewById(R.id.true_button);
        mTrueButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                checkAnswer(true);
            }
        });

        mFalseButton = (Button) findViewById(R.id.false_button);
        mFalseButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                checkAnswer(false);
            }
        });

        mNextButton = (ImageButton) findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                mCurrentIndex = (mCurrentIndex+1)%mQuestionBank.size();/*length;*/
                mIsCheater = false;
                checkButtonEnable();
                updateQuestion();
            }
        });

        mPrevButton = (ImageButton) findViewById(R.id.prev_button);
        mPrevButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                mCurrentIndex =(mCurrentIndex+mQuestionBank.size()-1)%mQuestionBank.size();/*(mCurrentIndex+mQuestionBank.length-1)%mQuestionBank.length;*/
                mIsCheater = false;
                checkButtonEnable();
                updateQuestion();
            }
        });

        mCheatButton = (Button)findViewById(R.id.cheat_button);
        mCheatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean answerIsTrue =(mQuestionBank.get(mCurrentIndex)).isAnswerTrue(); /*mQuestionBank[mCurrentIndex].isAnswerTrue();*/
                Intent intent = CheatActivity.newIntent(QuizActivity.this, answerIsTrue);
                startActivityForResult(intent, REQUEST_CODE_CHEAT);
            }
        });

        mSubmitButton = (Button) findViewById(R.id.submit_button);
        mSubmitButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                submit();
            }
        });
        checkButtonEnable();
        updateQuestion();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == REQUEST_CODE_CHEAT) {
            if (data == null) {
                return;
            }
            mIsCheater = CheatActivity.wasAnswerShown(data);
            if (mIsCheater) {
                if(mQuestionBankstate[mCurrentIndex] == true){
                mAnswerNum += 1;
                mQuestionBankstate[mCurrentIndex] = false;
                checkButtonEnable();
                int messageResId = R.string.judgment_toast;
                Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();}
            }
        }
    }

    @Override
    public void onStart(){
        super.onStart();
        Log.d(TAG,"onStart() called");
    }

    @Override
    public void onResume(){
        super.onResume();
        Log.d(TAG,"onResume() called");
    }

    @Override
    public void onPause(){
        super.onPause();
        Log.d(TAG,"onPause() called");
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        Log.i(TAG,"onSaveInstanceState--mCurrentIndex");
        savedInstanceState.putInt(KEY_INDEX,mCurrentIndex);
        Log.i(TAG,"onSaveInstanceState--[]");
        savedInstanceState.putBooleanArray(KEY_QUESTIONSTATE,mQuestionBankstate);
        Log.i(TAG,"onSaveInstanceState--mCorrectNum");
        savedInstanceState.putInt(KEY_CORRECTNUM,mCorrectNum);
        Log.i(TAG,"onSaveInstanceState--mAnswerNum");
        savedInstanceState.putInt(KEY_ANSWERNUM,mAnswerNum);
        savedInstanceState.putSerializable(KEY_PROBLEM,mQuestionBank);
    }

    @Override
    public void onStop(){
        super.onStop();
        Log.d(TAG,"onStop() called");
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"onDestroy() called");
    }

    private void updateQuestion(){
        String question ="("+(mCurrentIndex+1)+"/"+mQuestionBank.size()+")"+(mQuestionBank.get(mCurrentIndex)).getTextResId();/* String.valueOf(mQuestionBank[mCurrentIndex].getTextResId()); */
        mQuestionTextView.setText(question);
    }

    private void checkAnswer(boolean userPressedTrue) {
        boolean answerIsTrue =(mQuestionBank.get(mCurrentIndex)).isAnswerTrue(); /*mQuestionBank[mCurrentIndex].isAnswerTrue();*/
        int messageResId = 0;
        if (mIsCheater) {
            messageResId = R.string.judgment_toast;
        } else {
            if (userPressedTrue == answerIsTrue) {
                messageResId = R.string.correct_toast;
                mCorrectNum += 1;
            } else {
                messageResId = R.string.incorrect_toast;
            }
        }
        mAnswerNum += 1;
        mQuestionBankstate[mCurrentIndex] = false;
        checkButtonEnable();
        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
//        Grade();
    }

    private void checkButtonEnable(){
        mTrueButton.setEnabled(mQuestionBankstate[mCurrentIndex]);
        mFalseButton.setEnabled(mQuestionBankstate[mCurrentIndex]);
    }

    private void Grade(){
//        if(mCurrentIndex==(mQuestionBank.length-1)){
            System.out.println("Result--mCorrectNum："+mCorrectNum);
            String grade = Float.toString(Float.valueOf(mCorrectNum)/mQuestionBank.size()*100);/*length*/
            Toast.makeText(this, "grade："+grade+" %", Toast.LENGTH_SHORT).show();
//        }
    }

    private void submit(){
        if(mAnswerNum==mQuestionBank.size()){/*length*/
            Grade();
        }else{
            int num = mQuestionBank.size()-mAnswerNum;/*length*/
            if(num==1){
                Toast.makeText(this, num+" question unanswered", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, num+" questions unanswered", Toast.LENGTH_SHORT).show();
            }
        }
    }
}